package com.huawei.hms.framework.network.restclient.dnkeeper;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.framework.common.CheckParamUtils;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsResult;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsResult.Address;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DNKeeper;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DomainResult;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.dnresolver.dnkeeper.DomainResult.Address.Builder;
import java.util.ArrayList;
import java.util.List;

public class DefaultDNKeeper implements DNKeeper {
    private static final String TAG = "DefaultDNKeeper";
    @SuppressLint({"StaticFieldLeak"})
    private static volatile DefaultDNKeeper instance;
    private Context context;

    private DefaultDNKeeper(Context context) {
        this.context = context.getApplicationContext();
        DNKeeperManager.getInstance().init(this.context);
    }

    public static DefaultDNKeeper getInstance(Context context) {
        CheckParamUtils.checkNotNull(context, "context == null");
        if (instance == null) {
            synchronized (DefaultDNKeeper.class) {
                if (instance == null) {
                    instance = new DefaultDNKeeper(context);
                }
            }
        }
        return instance;
    }

    public DefaultDNKeeper init(int i) {
        DNKeeperManager.getInstance().init(this.context, i);
        return this;
    }

    public void forceUseDnkeeper(String str, int i) {
        if (!TextUtils.isEmpty(str)) {
            RequestHost requestHost = new RequestHost(str);
            requestHost.setDnsFailType("" + i);
            DNKeeperManager.getInstance().queryIpsAsync(requestHost, new 1(this));
        }
    }

    public List<String> getDns(String str, String str2, int i) {
        List<String> arrayList = new ArrayList();
        if (TextUtils.isEmpty(str)) {
            return arrayList;
        }
        DnsResult queryIpsFromCache;
        if (i == 0) {
            queryIpsFromCache = DNKeeperManager.getInstance().queryIpsFromCache(str);
        } else {
            RequestHost requestHost = new RequestHost(str);
            requestHost.setFailIP(str2);
            requestHost.setDnsFailType("" + i);
            queryIpsFromCache = DNKeeperManager.getInstance().queryIpsSync(requestHost);
        }
        if (queryIpsFromCache != null) {
            for (Address value : queryIpsFromCache.getAddressList()) {
                arrayList.add(value.getValue());
            }
        }
        return arrayList;
    }

    public boolean removeDns(String str) {
        return DNKeeperManager.getInstance().removeCache(str);
    }

    public DomainResult queryIpsSync(String str, String str2, int i) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        RequestHost requestHost = new RequestHost(str);
        requestHost.setFailIP(str2);
        requestHost.setDnsFailType("" + i);
        return changeToDns(DNKeeperManager.getInstance().queryIpsSync(requestHost));
    }

    public void setRequestIntervalFailed(int i) {
        DNKeeperManager.getInstance().setRequestIntervalFailed(i);
    }

    public String getDomainName() {
        return DNKeeperManager.getInstance().getDomainName();
    }

    private DomainResult changeToDns(DnsResult dnsResult) {
        DomainResult domainResult = new DomainResult();
        if (!DNKeeperUtil.isIpListEmpty(dnsResult)) {
            domainResult.setType(dnsResult.getType());
            domainResult.setCreateTime(dnsResult.getCreateTime());
            for (Address address : dnsResult.getAddressList()) {
                domainResult.addAddress(new Builder().value(address.getValue()).ttl(address.getTtl()).type(address.getType()).build());
            }
        }
        return domainResult;
    }
}
